package com.zenithmind.document.pojo.query;

import com.zenithmind.common.pojo.query.BaseQueryDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文档统计查询DTO
 * 遵循单一职责原则：专注于统计查询条件
 * 遵循开闭原则：支持查询条件的扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Schema(description = "文档统计查询DTO")
public class DocumentStatisticsQueryDTO extends BaseQueryDTO {

    @Schema(description = "文档ID", example = "doc123456")
    private String documentId;

    @Schema(description = "文档ID列表")
    private List<String> documentIds;

    @Schema(description = "最小查看次数", example = "100")
    private Long minViewCount;

    @Schema(description = "最大查看次数", example = "1000")
    private Long maxViewCount;

    @Schema(description = "最小下载次数", example = "10")
    private Long minDownloadCount;

    @Schema(description = "最大下载次数", example = "100")
    private Long maxDownloadCount;

    @Schema(description = "最小分享次数", example = "5")
    private Long minShareCount;

    @Schema(description = "最大分享次数", example = "50")
    private Long maxShareCount;

    @Schema(description = "最小收藏次数", example = "5")
    private Long minFavoriteCount;

    @Schema(description = "最大收藏次数", example = "50")
    private Long maxFavoriteCount;

    @Schema(description = "最小评论次数", example = "1")
    private Long minCommentCount;

    @Schema(description = "最大评论次数", example = "20")
    private Long maxCommentCount;

    @Schema(description = "最小点赞次数", example = "5")
    private Long minLikeCount;

    @Schema(description = "最大点赞次数", example = "100")
    private Long maxLikeCount;

    @Schema(description = "最小评分次数", example = "3")
    private Long minRatingCount;

    @Schema(description = "最大评分次数", example = "30")
    private Long maxRatingCount;

    @Schema(description = "最小平均评分", example = "3.0")
    private Double minAverageRating;

    @Schema(description = "最大平均评分", example = "5.0")
    private Double maxAverageRating;

    @Schema(description = "最小独立访客数", example = "50")
    private Long minUniqueVisitors;

    @Schema(description = "最大独立访客数", example = "500")
    private Long maxUniqueVisitors;

    @Schema(description = "最小总访问时长（秒）", example = "1800")
    private Long minTotalViewDuration;

    @Schema(description = "最大总访问时长（秒）", example = "18000")
    private Long maxTotalViewDuration;

    @Schema(description = "最小平均访问时长（秒）", example = "60")
    private Double minAverageViewDuration;

    @Schema(description = "最大平均访问时长（秒）", example = "600")
    private Double maxAverageViewDuration;

    @Schema(description = "最小跳出率", example = "0.1")
    private Double minBounceRate;

    @Schema(description = "最大跳出率", example = "0.5")
    private Double maxBounceRate;

    @Schema(description = "最小转化率", example = "0.05")
    private Double minConversionRate;

    @Schema(description = "最大转化率", example = "0.3")
    private Double maxConversionRate;

    @Schema(description = "统计类型", example = "DAILY", allowableValues = {"DAILY", "WEEKLY", "MONTHLY", "YEARLY", "TOTAL"})
    private String statisticsType;

    @Schema(description = "统计类型列表")
    private List<String> statisticsTypes;

    @Schema(description = "数据来源", example = "SYSTEM", allowableValues = {"SYSTEM", "MANUAL", "IMPORT", "SYNC"})
    private String dataSource;

    @Schema(description = "数据来源列表")
    private List<String> dataSources;

    @Schema(description = "统计日期开始", example = "2025-01-01T00:00:00")
    private LocalDateTime statisticsDateStart;

    @Schema(description = "统计日期结束", example = "2025-01-31T23:59:59")
    private LocalDateTime statisticsDateEnd;

    @Schema(description = "最后查看时间开始", example = "2025-01-01T00:00:00")
    private LocalDateTime lastViewTimeStart;

    @Schema(description = "最后查看时间结束", example = "2025-01-31T23:59:59")
    private LocalDateTime lastViewTimeEnd;

    @Schema(description = "创建时间开始", example = "2025-01-01T00:00:00")
    private LocalDateTime createTimeStart;

    @Schema(description = "创建时间结束", example = "2025-01-31T23:59:59")
    private LocalDateTime createTimeEnd;

    @Schema(description = "更新时间开始", example = "2025-01-01T00:00:00")
    private LocalDateTime updateTimeStart;

    @Schema(description = "更新时间结束", example = "2025-01-31T23:59:59")
    private LocalDateTime updateTimeEnd;

    @Schema(description = "扩展数据关键词", example = "mobile")
    private String extendedDataKeyword;

    @Schema(description = "备注关键词", example = "系统")
    private String remarksKeyword;

    @Schema(description = "排序字段", example = "viewCount", allowableValues = {
            "viewCount", "downloadCount", "shareCount", "favoriteCount", "averageRating", 
            "uniqueVisitors", "statisticsDate", "createTime"
    })
    private String sortField;

    @Schema(description = "排序方向", example = "DESC", allowableValues = {"ASC", "DESC"})
    private String sortDirection;

    @Schema(description = "分组字段", example = "statisticsType", allowableValues = {
            "statisticsType", "dataSource", "documentId"
    })
    private String groupBy;

    @Schema(description = "统计类型", example = "SUM", allowableValues = {"COUNT", "SUM", "AVG", "MAX", "MIN"})
    private String aggregationType;

    @Schema(description = "时间聚合粒度", example = "DAY", allowableValues = {"HOUR", "DAY", "WEEK", "MONTH", "YEAR"})
    private String timeGranularity;

    @Schema(description = "是否只查询热门文档", example = "false")
    private Boolean onlyPopular;

    @Schema(description = "是否只查询高评分文档", example = "false")
    private Boolean onlyHighRated;

    @Schema(description = "是否只查询最新统计", example = "true")
    private Boolean onlyLatest;

    /**
     * 验证查询条件
     */
    public boolean isValid() {
        // 查看次数范围验证
        if (minViewCount != null && maxViewCount != null) {
            if (minViewCount > maxViewCount || minViewCount < 0) return false;
        }
        
        // 下载次数范围验证
        if (minDownloadCount != null && maxDownloadCount != null) {
            if (minDownloadCount > maxDownloadCount || minDownloadCount < 0) return false;
        }
        
        // 评分范围验证
        if (minAverageRating != null && maxAverageRating != null) {
            if (minAverageRating > maxAverageRating || minAverageRating < 0 || maxAverageRating > 5) return false;
        }
        
        // 时长范围验证
        if (minTotalViewDuration != null && maxTotalViewDuration != null) {
            if (minTotalViewDuration > maxTotalViewDuration || minTotalViewDuration < 0) return false;
        }
        
        // 比率范围验证
        if (minBounceRate != null && maxBounceRate != null) {
            if (minBounceRate > maxBounceRate || minBounceRate < 0 || maxBounceRate > 1) return false;
        }
        
        if (minConversionRate != null && maxConversionRate != null) {
            if (minConversionRate > maxConversionRate || minConversionRate < 0 || maxConversionRate > 1) return false;
        }
        
        // 时间范围验证
        if (statisticsDateStart != null && statisticsDateEnd != null) {
            if (statisticsDateStart.isAfter(statisticsDateEnd)) return false;
        }
        
        return true;
    }

    /**
     * 是否有计数范围查询
     */
    public boolean hasCountRangeQuery() {
        return minViewCount != null || maxViewCount != null ||
               minDownloadCount != null || maxDownloadCount != null ||
               minShareCount != null || maxShareCount != null ||
               minFavoriteCount != null || maxFavoriteCount != null ||
               minCommentCount != null || maxCommentCount != null ||
               minLikeCount != null || maxLikeCount != null ||
               minRatingCount != null || maxRatingCount != null ||
               minUniqueVisitors != null || maxUniqueVisitors != null;
    }

    /**
     * 是否有评分范围查询
     */
    public boolean hasRatingRangeQuery() {
        return minAverageRating != null || maxAverageRating != null;
    }

    /**
     * 是否有时长范围查询
     */
    public boolean hasDurationRangeQuery() {
        return minTotalViewDuration != null || maxTotalViewDuration != null ||
               minAverageViewDuration != null || maxAverageViewDuration != null;
    }

    /**
     * 是否有比率范围查询
     */
    public boolean hasRateRangeQuery() {
        return minBounceRate != null || maxBounceRate != null ||
               minConversionRate != null || maxConversionRate != null;
    }

    /**
     * 是否有时间范围查询
     */
    public boolean hasTimeRangeQuery() {
        return statisticsDateStart != null || statisticsDateEnd != null ||
               lastViewTimeStart != null || lastViewTimeEnd != null ||
               createTimeStart != null || createTimeEnd != null ||
               updateTimeStart != null || updateTimeEnd != null;
    }

    /**
     * 是否有关键词查询
     */
    public boolean hasKeywordQuery() {
        return extendedDataKeyword != null || remarksKeyword != null;
    }

    /**
     * 获取默认排序
     */
    public String getDefaultSort() {
        return "statisticsDate DESC, viewCount DESC";
    }

    /**
     * 是否查询热门文档
     */
    public boolean isQueryPopular() {
        return onlyPopular != null && onlyPopular;
    }

    /**
     * 是否查询高评分文档
     */
    public boolean isQueryHighRated() {
        return onlyHighRated != null && onlyHighRated;
    }

    /**
     * 是否查询最新统计
     */
    public boolean isQueryLatest() {
        return onlyLatest != null && onlyLatest;
    }

    /**
     * 是否为复杂查询
     */
    public boolean isComplexQuery() {
        return hasCountRangeQuery() || hasRatingRangeQuery() || hasDurationRangeQuery() ||
               hasRateRangeQuery() || hasTimeRangeQuery() || hasKeywordQuery();
    }

    /**
     * 获取热门文档阈值
     */
    public Long getPopularThreshold() {
        // 定义热门文档的查看次数阈值
        return 1000L;
    }

    /**
     * 获取高评分阈值
     */
    public Double getHighRatingThreshold() {
        // 定义高评分文档的评分阈值
        return 4.0;
    }
}
